/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.web.controller;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.cx1.model.User;
import org.cx1.model.utils.ServiceResponse;
import org.cx1.model.utils.SystemProperties;
import org.cx1.service.api.IAuthService;
import org.cx1.service.api.IEmailService;
import org.cx1.service.api.IUserService;
import org.cx1.service.impl.DAuthService;
import org.cx1.service.impl.DUserService;
import org.cx1.service.impl.MEmailService;
import org.cx1.service.utils.UAgentInfo;
import org.cx1.web.utils.SessionUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.view.RedirectView;

/**
 *
 * @author ECH
 */
public class PasswordController extends BaseController {
    
    //private IAuthService loginService = new MAuthService();
    private IAuthService loginService = new DAuthService();
    private IUserService userService = new DUserService();
    private IEmailService emailService = new MEmailService();
    
    public PasswordController() {
        
    }
    
    protected ModelAndView handleRequestInternal( HttpServletRequest request,
                                                  HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView();
        Map map = new HashMap();
        
        int action = StringUtils.isEmpty( request.getParameter("a") ) ? 0 : Integer.parseInt(request.getParameter("a"));
        
        switch (action) {
            default: {
                retrievePassword(mav, map, request);
                break;
            }
        }        
        
        map.put("host", SystemProperties.SYSTEM_HOST);
        map.put("port", SystemProperties.SYSTEM_PORT);
        map.put("props", this.getSystemProperties());
        mav.addObject("model", map);
        return mav;
    }
    
    private void retrievePassword(ModelAndView mav, Map map, HttpServletRequest request) {
        if (!StringUtils.isEmpty(request.getParameter("s"))) {
            String email = request.getParameter("email");
            ServiceResponse<Boolean> response = emailService.sendLoginDetails(email);
            
            if (response.getResponse()) {
                map.put("success", "1");
                if (isThisRequestCommingFromAMobileDevice(request)) {
                    mav.setViewName("password_mobile");
                }
                else {
                    mav.setViewName("password");                
                }
            }            
            else {
                map.put("success", "0");
                if (isThisRequestCommingFromAMobileDevice(request)) {
                    mav.setViewName("password_mobile");
                }
                else {
                    mav.setViewName("password");                
                }
            }
        }
        else {
            map.put("submit", "0");
            
            if (isThisRequestCommingFromAMobileDevice(request)) {
                mav.setViewName("password_mobile");
            }
            else {
                mav.setViewName("password");                
            }
        }
    }
    
}